package com.ff.xyh.access.controller.access;

import com.ff.xyh.access.service.access.AccessPermissionService;
import com.ff.xyh.access.service.access.AccessUserService;
import com.ff.xyh.common.entity.result.ResultMessage;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.entity.useraccess.AccessPermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Auther FF
 * @Date 2023年 01月 15日
 * @Time 07:59
 */

@Api(tags = "后台认证接口")
@RestController
@RequestMapping("/access")
public class AccessController {

    @Autowired
    private AccessPermissionService accessPermissionService;
    @Autowired
    private AccessUserService accessUserService;

    @ApiOperation(value = "获取当前登录的用户的权限列表")
    @GetMapping("menu")
    public ResultObject getMenu(){
        /** 要搞清楚在哪里封装 */
        /** 这些数据在 TokenAuthenticationFilter.getAuthentication 方法 返回语句时封装 */
        System.out.println(" getName ===> 实际上是id： " + SecurityContextHolder.getContext().getAuthentication().getName());
        System.out.println(" getAuthorities ===> 权限值列表： " + SecurityContextHolder.getContext().getAuthentication().getAuthorities());
        System.out.println(" getPrincipal ===>  又是id： " + SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        System.out.println(" getCredentials ===> token： " + SecurityContextHolder.getContext().getAuthentication().getCredentials().toString());
        System.out.println(" getDetails ===> 无 " + SecurityContextHolder.getContext().getAuthentication().getDetails());
        //获取当前登录用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        List<AccessPermission> permissionList = accessPermissionService.getMenu(username);
        if (permissionList == null || permissionList.isEmpty()) {
            return ResultObject.error().message("No Any Permission");
        }
        return ResultObject.ok().data("permissionList", permissionList);
    }
    /**
     * 通过请求头token查询用户基础信息以及角色和权限的信息
     * @param request
     * @return
     */
    @ApiOperation(value = "通过请求头中的token获取当前用户基本信息")
    @GetMapping("info")
    public ResultObject getInfo (HttpServletRequest request) {
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            // 请求头里没有则获取当前登录的
            token = SecurityContextHolder.getContext().getAuthentication().getCredentials().toString();
        }
//        System.out.println("getInfo ==> " + token);
        Map<String, Object> userInfo = accessUserService.getUserInfo(token);
        if (userInfo.isEmpty()) {
            return ResultObject.error().message(ResultMessage.NO_RESULT);
        }
        return ResultObject.ok().data("data", userInfo);
    }

//    @ApiOperation(value = "获取当前用户基本信息 在请求体中传入token")
//    @GetMapping("info2")
//    public ResultObject getInfo2 (
//            @RequestParam(name = "token") String token) {
//        System.out.println("getInfo ==> " + token);
//        Map<String, Object> userInfo = accessUserService.getUserInfo(token);
//        if (userInfo.isEmpty()) {
//            return ResultObject.error().message(ResultMessage.NO_RESULT);
//        }
//        return ResultObject.ok().data("data", userInfo);
//    }


}
